﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Windows;
using System.Runtime.InteropServices;
using SlimDX.Windows;
using SCS.GFX;
using SlimDX;


namespace SCS
{
    static class Program
    {
        static Form RenderForm;
        static RenderInfo RenderInfo;

        [STAThread]
        static void Main()
        {
            Logger.EraseLog();
            Logger.WriteMsg("Application startup");
            
            RenderForm = new Form();
            RenderForm.ClientSize = new Size(800, 600);
            RenderForm.MouseDoubleClick += new MouseEventHandler(RenderForm_MouseDoubleClick);
            RenderForm.KeyDown += new KeyEventHandler(RenderForm_KeyDown);
            RenderForm.Text = "Space Combat Sim";

            RenderInfo = new RenderInfo();

            Renderer.Initialise(RenderForm);
            Models.Load();
            Game.Initialise();

            

            Application.EnableVisualStyles();
            Application.Idle += new EventHandler(Application_Idle);
            Logger.WriteMsg("Application run");
            Application.Run(RenderForm);

            Logger.WriteMsg("Application shutdown");
            Game.Shutdown();
            Renderer.Shutdown();
            Logger.WriteMsg("Application done !");
        }

        static void RenderForm_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1: Logger.WriteMsg("F1");
                    break;
                case Keys.Up: Renderer.Camera.MoveForward(0.5f);
                    break;
                case Keys.Down: Renderer.Camera.MoveBackward(0.5f);
                    break;
                case Keys.Left: Renderer.Camera.MoveLeft(0.5f);
                    break;
                case Keys.Right: Renderer.Camera.MoveRight(0.5f);
                    break;
                case Keys.Home: Renderer.Camera.MoveUp(0.5f); 
                    break;
                case Keys.End: Renderer.Camera.MoveDown(0.5f); 
                    break;
                case Keys.Delete: Renderer.Camera.RotateLeft(0.5f);
                    break;
                case Keys.PageDown: Renderer.Camera.RotateRight(0.5f);
                    break;

                default:
                    break;
            }

        }

        static void Application_Idle(object sender, EventArgs e)
        {
            
            while (MessagePump.IsApplicationIdle)
            {
                RenderInfo.FrameNumber++;

                

                Game.Update(RenderInfo);
                Renderer.Render(RenderInfo);
            }
        }
            

        static void RenderForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Logger.WriteError("app shutdown event given", sender.GetType().ToString());
            Application.Exit();
        }
    }
}
